package com.onlyphoto.onlymall.mvp.view.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.bean.response.AddressBean
import com.onlyphoto.onlymall.listeners.NoDoubleClickListener
import com.onlyphoto.onlymall.mvp.contract.CShippingAddress
import com.onlyphoto.onlymall.mvp.presenter.PShippingAddressImpl
import com.onlyphoto.onlymall.mvp.view.binder.AddressBinder
import com.onlyphoto.onlymall.utils.NetWorkStatus
import kotlinx.android.synthetic.main.activity_shippingaddress.*
import kotlinx.android.synthetic.main.activity_shippingaddress.back
import me.bakumon.statuslayoutmanager.library.DefaultOnStatusChildClickListener

import me.bakumon.statuslayoutmanager.library.StatusLayoutManager
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter


class ShippingAddressActivity : BaseActivity<PShippingAddressImpl>(), CShippingAddress.IVShippingAddress {

    var adapter: MultiTypeAdapter? = null
    var layoutManager: LinearLayoutManager? = null
    private var items: Items? = null
    private var canBack = false
    var addressBean: AddressBean? = null
    companion object {
        const val REQUEST_ORDER_CONFIRM = 500
    }

    override fun retry() {}

    override fun loadData() {
        mPresenter?.getAddress()
    }

    override fun init() {
        canBack = intent.getBooleanExtra("canBack", false)
        initRv()
    }

    private fun initRv(){
        adapter = MultiTypeAdapter()
        layoutManager = LinearLayoutManager(this)
        layoutManager?.orientation = RecyclerView.VERTICAL
        var binder = AddressBinder()
        if (canBack) {
            binder.setResultListener(object :AddressBinder.ResultListener{
                override fun callBack(bean: AddressBean) {
                    addressBean = bean
                    val intent = Intent()
                    intent.putExtra("result", addressBean?.id)
                    setResult(Activity.RESULT_OK, intent)
                    finish()
                }

            })
            binder.isOrderConfirm(true)
        }
        adapter?.register(AddressBean::class.java, binder)
        rv_address.layoutManager = layoutManager

        rv_address.adapter = adapter

        items = Items()

        adapter?.items = items!!
    }

    override fun initListener() {
        add_address.setOnClickListener(object : NoDoubleClickListener(){
            override fun onNoDoubleClick(view: View?) {
                startActivity(Intent(this@ShippingAddressActivity, EditAddressActivity::class.java))
            }

        })

        back.setOnClickListener{
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    override fun updateView() {
        showSuccess()
    }

    override fun showEmpty() {
        items?.clear()
        commonStatusView!!.setContent(R.mipmap.no_details, "收货地址为空～", "")
        commonStatusView!!.setType(NetWorkStatus.EMPTY)
        showCustomLayout(commonStatusView!!, R.id.ll_content)
    }

    public override fun initCustomerStatusManager() {
        builder = StatusLayoutManager.Builder(rv_address)
                .setDefaultLoadingText("数据加载中...")
                .setOnStatusChildClickListener(object : DefaultOnStatusChildClickListener() {
                    override fun onCustomerChildClick(view: View) {
                        super.onCustomerChildClick(view)
                        when (view.id) {
                            R.id.ll_content -> {
                                when {
                                    view.tag == NetWorkStatus.FAIL -> retry()
                                    view.tag == NetWorkStatus.ERROR -> retry()
                                    view.tag == NetWorkStatus.EMPTY -> {
                                    }
                                }
                            }
                            else -> retry()
                        }
                    }
                })
                .setDefaultLayoutsBackgroundColor(Color.WHITE)
        statusLayoutManager = builder!!.build()
    }

    override fun setContentViewId(): Int {
        return R.layout.activity_shippingaddress
    }

    override fun getAddressSuccess(list: ArrayList<AddressBean>) {
        if (items?.size!! > 0) {
            items?.clear()
        }
        items?.addAll(list)
        adapter?.notifyDataSetChanged()
    }

    override fun createPresenter() {
        mPresenter = PShippingAddressImpl(mContext, this)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data != null) {
            if (requestCode == REQUEST_ORDER_CONFIRM) {
                val intent = Intent()
                intent.putExtra("result", data.getIntExtra("result", 0))
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        }
    }
}
